package com.shop.seller.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.shop.mapper.SpecificationOptionMapper;
import com.shop.mapper.TypeTemplateMapper;
import com.shop.model.SpecificationOption;
import com.shop.model.SpecificationOptionExample;
import com.shop.model.TypeTemplate;
import com.shop.seller.service.SpecificationService;

/**
 * 规格选项接口实现类
 * @author Y
 *
 */
@Service("specificationService")
public class SpecificationServiceBean implements SpecificationService{
	@Resource
	private SpecificationOptionMapper specificationOptionMapper;

	@Resource
	private TypeTemplateMapper typeTemplateMapper;
	
	
	@Override
	public List<Map> findSpecList(Long typeTemplateid) throws Exception {
		//根据模板id查找模板
		TypeTemplate typeTemplate=typeTemplateMapper.selectByPrimaryKey(typeTemplateid);
		List<Map> list=null;
		if(typeTemplate!=null)
		{
			//查出模板中以json字符串形式存储的关联的规格id和规格名称，转成map集合
			 list=JSON.parseArray(typeTemplate.getSpecIds(), Map.class);
			for(Map map:list)
			{
				SpecificationOptionExample example=new SpecificationOptionExample();
				com.shop.model.SpecificationOptionExample.Criteria criteria=example.createCriteria();
				criteria.andSpecIdEqualTo(new Long((Integer)map.get("id")));
				//用规格id查找该规格下的所有选项
				List<SpecificationOption> Options=specificationOptionMapper.selectByExample(example);
				//把查到的选项存进map
				map.put("options", Options);
			}
		}
		//返回集合
		return list;
	}

}
